package caat.application.managedbean;

import caat.application.dao.AdminDAO;
import caat.application.entityobject.AdminEO;
import caat.framework.AppAction;
import caat.framework.AppConstants;
import caat.framework.datamanager.Parameter;
import java.io.Serializable;
import java.sql.Connection;
import java.util.Vector;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

@SuppressWarnings("serial")
public class AdminLoginMB extends AppAction implements Serializable {
	/*
	 * Khai bao cac bien su dung de login
	 */
	private String username_;
	private String password_;
	private boolean bLoggedIn;
	private static String AUTH_KEY = "adminLoginMB";

	public boolean isbLoggedIn() {
		return bLoggedIn;
	}

	public void setbLoggedIn(boolean bLoggedIn) {
		this.bLoggedIn = bLoggedIn;
	}

	public String getUsername_() {
		return username_;
	}

	public void setUsername_(String username_) {
		this.username_ = username_;
	}

	public String getPassword_() {
		return password_;
	}

	public void setPassword_(String password_) {
		this.password_ = password_;
	}

	public AdminLoginMB() {
		super();
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String doLogin() {
		Connection conn = null;
		Vector params = null;
		StringBuffer whereClause = null;
		AdminDAO adminDAO = null;
		AdminEO adminEO = null;
		String result = AppConstants.ACTION_FAILURE;
		try {
			conn = getConnection();
			if (conn != null) {
				// khoi tao cac bien su dung trong method
				whereClause = new StringBuffer();
				params = new Vector();
				adminDAO = new AdminDAO(conn);
				// kiem tra neu nhap user va pass
				if (username_ != null && password_ != null
						&& !AppConstants.STRING_EMPTY.equals(username_.trim())
						&& !AppConstants.STRING_EMPTY.equals(password_.trim())) {
					whereClause.append("a.username = ? and a.password=?");
					params.add(new Parameter(username_, true));
					params.add(new Parameter(password_, true));
					adminEO = adminDAO.getAdmin(whereClause.toString(), params);
					if (adminEO != null) {
						bLoggedIn = true;
						}
				}
				// kiem tra neu true thi login success
				if (bLoggedIn) {
					result = AppConstants.ACTION_SUCCESS;
				} else {
					// user khong ton tai
					// if(username_!=null){
					// String errorSummary= "User Fail!";
					// String errorDetail = "User Fail!";
					// displayError(errorSummary, errorDetail);
					// }
					String errorSummary = "Login Fail!";
					String errorDetail = "Login Fail!";
					displayError(errorSummary, errorDetail);
				}
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			result = "errors";
		}
		return result;
	}

	public String logout() {
		String strResult = null;
		Object objResult = FacesContext.getCurrentInstance().getExternalContext()
				.getSessionMap().remove(AUTH_KEY);
		if(objResult!=null){
			strResult = "logout";
		}
		return strResult;
	}

	// check confirmpassword (add new)
//	public void mvalidatePass(FacesContext context, UIComponent component,
//			Object value) throws ValidatorException {
//		FacesMessage msg;
//		password_ = value.toString();
//		if (AppConstants.STRING_EMPTY.equals(password_)) {
//			msg = new FacesMessage("Phải nhập mật khẩu");
//			throw new ValidatorException(msg);
//		}
//	}
}
